package com.cd.cms.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DateUtil {
	
	/**
	 * yyyy-MM-dd
	 */
	public static final String FORMAT_1="yyyy-MM-dd";
	/**
	 * yyyy-MM-dd HH:mm:ss
	 */
	public static final String FORMAT_2="yyyy-MM-dd HH:mm:ss";	
	/**
	 * yyyy-MM-dd HH:mm
	 */
	public static final String FORMAT_3="yyyy-MM-dd HH:mm";
	/**
	 * yyyy年MM月dd日
	 */
	public static final String FORMAT_4="yyyy年MM月dd日";
	/**
	 * yyyyMMddHHmmss
	 */
	public static final String FORMAT_5="yyyyMMddHHmmss";
	/**
	 * yyyy
	 */
	public static final String FORMAT_YEAR="yyyy";
	/**
	 * 闭市时间
	 */
	public static final String END_TIME = "15:00:00";
	
	/**
	 * 日期格式："yyyy-MM-dd HH:mm:ss"
	 * 
	 * @return
	 */
	public static String getDateTime() {
		SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return tempDate.format(new java.util.Date());
	}
	
	public static String getCustomDateTime() {
		SimpleDateFormat tempDate = new SimpleDateFormat("yyyyMMddHHmmss");
		return tempDate.format(new java.util.Date());
	}
	
	
	/**获取当天交易结束时间
	 * @return
	 * @throws ParseException
	 */
	public static Date getTradeEndTime() throws ParseException
	{
		Date date=DateUtil.getDateTime(DateUtil.getDate() + " " + END_TIME, DateUtil.FORMAT_2);
		return date;
		
	}
	
	/**
	 * 字符串转换日期
	 * @param date
	 * @param format
	 * @return
	 * @throws ParseException
	 */
	public static Date getDateTime(String date,String format) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.parse(date);
	}
	
	
	/**
	 * 将yyyyMMdd转换成yyyy-MM-dd
	 * @param date
	 * @return
	 */
	public static String standardDate(String date) {
		if (date== null || date.length() != 8)
			return date;
		return date.substring(0, 4) + "-" + date.substring(4, 6)
				+ "-" + date.substring(6, 8);
	}
	
	public static String standardDate(Date date) {
		if (date== null)
			return null;
		
		SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd");
		return tempDate.format(date);
	}
	
	
	public static String standardDate(Date date,String format) {
		if (date== null)
			return null;
		
		SimpleDateFormat tempDate = new SimpleDateFormat(format);
		return tempDate.format(date);
	}
	
	/**
	 * 
	 * @param date
	 * 将yyyyMMdd转换成yyyy-MM-dd
	 * @param time
	 * 将HHmmss转换成HH:mm:ss
	 * @return
	 */
	public static String standardDateTime(String date, String time) {
		return String.format("%s %s", standardDate(date), standardTime(time));
	}
	
	public static String format(Date date, String pattern) {
		if (date== null)
			return null;
		
		SimpleDateFormat tempDate = new SimpleDateFormat(pattern);
		return tempDate.format(date);
	}
	
	public static Date format(String date, String pattern) throws ParseException {
		if (StringUtils.isEmpty(date))
			return null;
		
		SimpleDateFormat tempDate = new SimpleDateFormat(pattern);
		return tempDate.parse(date);
	}
	
	/**
	 * 将HHmmss转换成HH:mm:ss
	 * @param date
	 * @return
	 */
	public static String standardTime(String time) {
		if (time == null || time.length() != 6)
			return time;
		return time.substring(0, 2) + ":" + time.substring(2, 4)
				+ ":" + time.substring(4, 6);
	}
	
	/**
	 * 将HHmmss转换成HH:mm
	 * @param date
	 * @return
	 */
	public static String standardShortTime(String time) {
		if (time == null || time.length() < 4)
			return time;
		return time.substring(0, 2) + ":" + time.substring(2, 4);
	}
	
	/**
	 * 将yyyy-MM-dd转换成yyyyMMdd
	 * @param date
	 * @return
	 */
	public static String customDate(String date) {
		if (date != null) {
			return date.replace("-", "");
		}
		return date;
	}
	
	/**
	 * 将HH:mm:ss转换成HHmmss
	 * @param date
	 * @return
	 */
	public static String customTime(String time) {
		if (time != null) {
			return time.replace(":", "");
		}
		return time;
	}
	
	public static String getYear() {
		SimpleDateFormat tempDate = new SimpleDateFormat("yy");
		return tempDate.format(new java.util.Date());
	}
	
	public static String getYear(String pattern) {
		SimpleDateFormat tempDate = new SimpleDateFormat(pattern);
		return tempDate.format(new java.util.Date());
	}
	
	public static String getYearAndMonth() {
		SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM");
		return tempDate.format(new java.util.Date());
	}
	
	public static String getYearAndMonth(Date date) {
		SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM");
		return tempDate.format(date);
	}
	
	public static String getDate() {
		SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd");
		return tempDate.format(new java.util.Date());
	}
	

	public static String getTime() {
		SimpleDateFormat tempDate = new SimpleDateFormat("HH:mm:ss");
		return tempDate.format(new java.util.Date());
	}
	
	public static String getCustomDate() {
		SimpleDateFormat tempDate = new SimpleDateFormat("yyyyMMdd");
		return tempDate.format(new java.util.Date());
	}
	
	public static int nowDateToInt() {
		return Integer.parseInt(getCustomDate());
	}
	
	public static String getCustomTime() {
		SimpleDateFormat tempDate = new SimpleDateFormat("HHmmss");
		return tempDate.format(new java.util.Date());
	}
	
	public static int nowTimeToInt() {
		return Integer.parseInt(getCustomTime());
	}

	/**
	 * 时间差
	 * @param current_time
	 * @param compare_time
	 * @return
	 */
	public static long getDateCompare(String current_time, String compare_time) {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long time = 0;
		try {
			Date c_tiem = sf.parse(current_time);
			Date com_time = sf.parse(compare_time);
			long l = c_tiem.getTime() - com_time.getTime() > 0 ? c_tiem
					.getTime() - com_time.getTime() : com_time.getTime()
					- c_tiem.getTime();
			time = l / 1000; // 算出超时秒数
		} catch (Exception e) {
			e.printStackTrace();
		}
		return time;
	}

	/**
	 * 处理时间的加减运算 60*60 为一个小时 60*60*24 为一天
	 * @param startTime
	 * @param endTime
	 * @param type 
	 *        0:加,1:减
	 * @return
	 */
	public static long getDateAdd(String startTime, String endTime, int type) {
		long time = 0l;
		try {
			Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
					.parse(startTime);
			Date addLong = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
					.parse(endTime);
			switch (type) {
			case 0:
				time = (date.getTime() / 1000) + (addLong.getTime() / 1000);
				break;
			case 1:
				time = (date.getTime() / 1000) - (addLong.getTime() / 1000);
				break;
			default:
				time = (date.getTime() / 1000) + (addLong.getTime() / 1000);
				break;
			}
			date.setTime(time * 1000);
			time = date.getTime();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return time;
	}

	/**
	 * 一个月最大day
	 * @param time
	 * @return
	 */
	public static Object[] getMaxMonth(String time) {
		Object[] obj = new Object[2];
		Date date = null;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar a = Calendar.getInstance();
		a.setTime(date);
		a.set(Calendar.DATE, 1); // 把日期设置为当月第一天
		a.roll(Calendar.DATE, -1);// 日期回滚一天，也就是最后一天
		int maxMonth = a.get(Calendar.DATE);
		a.roll(Calendar.DATE, 1);
		// System.out.println("该月最大天数:"+maxMonth+",某月="+a.get(Calendar.MONTH));
		time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(a.getTime());
		// System.out.println("time="+time);
		obj[0] = maxMonth;
		obj[1] = time;
		return obj;
	}
	
	/**
	 * 当前日期加减n天
	 * @param n
	 * @return
	 */
	public static String nowDateAddDays(int n)   {  
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");  
        Calendar now = Calendar.getInstance();    
        now.add(Calendar.DAY_OF_MONTH, n);
        return df.format(now.getTime()); 
    }
	
	public static String nowDateAddDays(int n, String pattern)   {  
        SimpleDateFormat df = new SimpleDateFormat(pattern);  
        Calendar now = Calendar.getInstance();    
        now.add(Calendar.DAY_OF_MONTH, n);
        return df.format(now.getTime()); 
    }
	
	public static String T1() {
		return DateUtil.nowDateAddDays(1, "MM月dd日") + " 16:00";
	}
	
	public static String T2() {
		return DateUtil.nowDateAddDays(2, "MM月dd日") + " 16:00";
	}
	public static String T3() {
		return DateUtil.nowDateAddDays(3, "MM月dd日") + " 16:00";
	}
	
	public static String DayT1() {
		return DateUtil.nowDateAddDays(1, "MM月dd日");
	}
	
	public static String DayT2() {
		return DateUtil.nowDateAddDays(2, "MM月dd日");
	}
	
	/**
	 * date加减n天
	 * @param date
	 * @param n
	 * @return
	 * @throws ParseException
	 */
	public static String dateAddDays(String date, int n) throws ParseException   {  
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date dt = df.parse(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dt);
        calendar.add(Calendar.DAY_OF_MONTH, n);
        return df.format(calendar.getTime());
    }
	
	
	
	/**
	 * 获取两个日期相隔年数
	 * @param startDate
	 * @param enddate
	 * @return
	 */
	public static double getDiffYears(Date startDate,Date enddate) 
	{
		Calendar starCal = Calendar.getInstance();
        starCal.setTime(startDate);

        int sYear = starCal.get(Calendar.YEAR);
        int sMonth = starCal.get(Calendar.MONTH);
        int sDay = starCal.get(Calendar.DATE);

        Calendar endCal = Calendar.getInstance();
        endCal.setTime(enddate);
        int eYear = endCal.get(Calendar.YEAR);
        int eMonth = endCal.get(Calendar.MONTH);
        int eDay = endCal.get(Calendar.DATE);
        
        double year=eYear-sYear;
        if(year!=0)
        {
        	if(eMonth>sMonth)
        	{
        		year=year+0.5;
        	}
        	else if(eMonth==sMonth)
        	{
        		if(eDay>sDay)
        		{
        			year=year+0.5;
        		}else if(eDay<sDay)
        		{
        			year=year-1;
        		}
        	}
        	else
        	{
        		year=year-1;
        	}
        }
        return year;
	}
	
	 /**
     * @param d
     * @return
     */
    public static String getQuarter(Date d){
    	Calendar cal=Calendar.getInstance();
    	cal.setTime(d);
    	String year=String.valueOf(cal.get(Calendar.YEAR));
    	String quater=String.valueOf(cal.get(Calendar.MONTH)%3==0?cal.get(Calendar.MONTH)/3:cal.get(Calendar.MONTH)/3+1);
    	String restr=String.format("%s年%s季", year.substring(2),quater);
		return restr;
    	
    }
    
    
    public static Date getDate(int n,String parttrn){
    	try {
			return DateUtil.getDateTime(DateUtil.nowDateAddDays(n),parttrn);
		} catch (ParseException e) {
			e.printStackTrace();
		}
    	return null;
    }
    
    public static Date getDateFormat(int n,String parttrn){
    	try {
			return DateUtil.getDateTime(DateUtil.nowDateAddDays(n,parttrn),parttrn);
		} catch (ParseException e) {
			e.printStackTrace();
		}
    	return null;
    }
}
